package com.fsq.controller;

import com.alibaba.fastjson.JSONObject;
import com.fsq.token.UserToken;
import com.fsq.util.JwtTokenUtil;
import com.fsq.util.WebContextUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fsq
 * @date 2020/07/28
 */
@RestController
public class LoginController {

    //TODO 没有把code做缓存
    @PostMapping("/login")
    public String login(@RequestBody UserToken userToken) {
        if (!ObjectUtils.isEmpty(userToken)
                && "18316102612".equals(userToken.getPhone())
                && "123456".equals(userToken.getCode())) {
            String context = JSONObject.toJSONString(userToken);
            String result = JwtTokenUtil.createToken(context);
            WebContextUtil.setUserToken(userToken.getPhone(),result);
            return result;
        }
        return null;
    }
}
